package com.example.demo.controller;


import com.example.demo.entity.Table;
import com.example.demo.entity.User;

import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    UserMapper userMapper;

    @RequestMapping("/showUser")
    public String getShowUser(){
        //默认前缀  是 resources/templates
        return "/user/showUser";
    }


    @GetMapping("/addUser")
    public String addUser(){
        //默认前缀  是 resources/templates
        return "/user/addUser";
    }

    @ResponseBody
    @PostMapping("/addUser")
    public  int addUserByAjax(@RequestParam("username") String username,
                              @RequestParam("sex") String sex){

           User user= new User();
           user.setName(username);
           user.setSex(sex);
           //返回数据库受影响的行数

          return userMapper.insert(user);
    }


    @ResponseBody
    @RequestMapping("/getAllUser")
    public Table getAllUser(){
        Table tab = new Table<User>();
        tab.setData(userMapper.selectList(null));
        tab.setCount(userMapper.selectList(null).size());
        return  tab;
    }

    @ResponseBody
    @RequestMapping("/deleteUserById")
    public int deleteUserById(@RequestParam("id") int id){
        //返回数据库受影响的行数
      return   userMapper.deleteById(id);
    }
}




